package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.domian.MiguMusicRetentAddTimer;
import org.jsbd.boss.service.IMiguMusicRetentAddTimerService;
import org.jsbd.boss.common.ResponseUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/miguMusicRetentAddTimer")
public class MiguMusicRetentAddTimerController {

	public static final Logger logger = LoggerFactory.getLogger(MiguMusicRetentAddTimerController.class);

	@Autowired
	private IMiguMusicRetentAddTimerService miguMusicRetentAddTimerService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "miguMusicRetentAddTimer/list";
	}

	@RequestMapping(value = "getList")
	@ResponseBody
	public Map<String, Object> getList(Integer miguMusicId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (miguMusicId != null) {
			MiguMusicRetentAddTimer miguMusicRetentAddTimer = new MiguMusicRetentAddTimer();
			miguMusicRetentAddTimer.setMiguMusicId(miguMusicId);
			List<MiguMusicRetentAddTimer> list = miguMusicRetentAddTimerService.getList(miguMusicRetentAddTimer);
			map.put("rows", list);
			map.put("total", 0);
		} else {
			List<MiguMusicRetentAddTimer> list = new ArrayList<>();
			list.add(new MiguMusicRetentAddTimer());
			map.put("rows", null);
			map.put("total", 0);
		}
		return map;
	}

	@RequestMapping(value = "/saveConfig")
	public void saveRetent(@RequestBody MiguMusicRetentAddTimer[] list, HttpServletResponse response) {
		if (list != null && list.length > 0) {
			Integer miguMusicId = list[0].getMiguMusicId();
			miguMusicRetentAddTimerService.deleteByMiguMusicId(miguMusicId);
		}
		miguMusicRetentAddTimerService.save(Arrays.asList(list));
		ResponseUtils.responseSuccess(response);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		try {
			List<Integer> list = Arrays.asList(ids);
			miguMusicRetentAddTimerService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}
	
}